package com.china08.yxyapi.api.manage.stu.mystudy.huixue.comm;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.questionbank.YxyQuesbankBook;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.huixue.comm.YxyQuesbankBookRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SemesterSrhVersionIdController extends YxyApiBaseController {

    @Autowired
    private YxyQuesbankBookRepository yqbr;

    @ApiOperation(value = "根据版本id获得学期(即册别)")
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/huixue/comm/srhsembyver", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(@RequestBody ReqModel reqModel) {
        List<YxyQuesbankBook> books = new ArrayList<YxyQuesbankBook>();
        List<RespModel> list = new ArrayList<SemesterSrhVersionIdController.RespModel>();
        books = yqbr.findByVersionIdAndStatusOrderByOrderNum(reqModel.version, 1);
        if (books.size() > 0) {
            for (YxyQuesbankBook book : books) {
                RespModel model = new RespModel();
                model.id = book.getId();
                model.name = book.getName();
                list.add(model);
            }
        }
        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // public String subject;
        public String version;
        // public String phase;

        @Override
        public void autoCheck() {
            // Assert.notNull(StringUtils.defaultIfBlank(subject, null),
            // "请指定学科");
            Assert.notNull(StringUtils.defaultIfBlank(version, null), "请指定版本");
            // Assert.notNull(StringUtils.defaultIfBlank(phase, null), "请指定学段");
        }

    }

    protected static class RespModel {
        public String id;
        public String name;
    }

}
